package com.sys.object.common.page;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
 * @author csm
 * @createDate 2010-07-19
 * @description 分页标签
 */
public class PageTagBoot extends SimpleTagSupport {

	/** url路径要带问号的，有参数的要带&号的 */
	private String url;
	private PageBean pageBean;
	private int tagnum;
	
	/**
	 * @return the tagnum
	 */
	public int getTagnum() {
		return tagnum;
	}

	/**
	 * @param tagnum the tagnum to set
	 */
	public void setTagnum(int tagnum) {
		this.tagnum = tagnum;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	/**
	 * @author cui
	 * @createDate 2011-7-19
	 * @return
	 * @throws JspException
	 */
	public void doTag() throws JspException, IOException {
		JspWriter out = this.getJspContext().getOut();
//		System.out.println(" pageTag pagebean is null:"+(pageBean==null));
		if(pageBean!=null){
		int currentPage = pageBean.getCurrentPage();
		int perpage = pageBean.getPerpage();
		int pageCount = pageBean.getPageCount();
//		int rowCount = pageBean.getRowCount();
		if (currentPage > pageCount) {
			currentPage = pageCount;
		}
		out.println("&nbsp;&nbsp;<form class='form-inline'>");
		if (currentPage <= 1) {
			out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>首&nbsp;&nbsp页</a>");
			out.println("&nbsp;&nbsp;");
			out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>上一页</a>");
		} else {
			out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage=1&perpage=" + perpage + " title=首页><i class='fa fa-backward'></i> 首&nbsp;&nbsp页</a>");
			out.println("&nbsp;&nbsp;");
			out.println("<a class='btn btn-xs btn-info' href="+ url + "currentPage="+ (currentPage - 1 + "") + "&perpage=" + perpage+ " title=上一页>上一页</a> ");
		}
		out.println("&nbsp;&nbsp;");
		if (currentPage >= pageCount) {
			if(pageCount>4){
				out.println("...");
				out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -4)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-4)+">"+(currentPage-4)+"</a> ");
				out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -3)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-3)+">"+(currentPage-3)+"</a> ");
				out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-2)+">"+(currentPage-2)+"</a> ");
				out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-1)+">"+(currentPage-1)+"</a> ");
				out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+currentPage+">"+currentPage+"</a> ");
				out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>下一页</a>");
				out.println("&nbsp;&nbsp;");
				out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>尾&nbsp;&nbsp页</a>");
			}else{
				for(int i=1;i<=pageCount;i++){
					if(i==currentPage){
						out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+i+">"+i+"</a> ");
					}else{
						out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ (i + "&perpage=" + perpage+ "") +  "  title="+i+">"+i+"</a> ");
					}
				}
				out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>下一页</a>");
				out.println("&nbsp;&nbsp;");
				out.println("<a class='btn btn-xs btn-default' href='javascript:void(0)'>尾&nbsp;&nbsp页</a>");
			}
		} else {
			if(pageCount<5){
				for(int i=1;i<=pageCount;i++){
					if(i==currentPage){
						out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+i+">"+i+"</a> ");
					}else{
						out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ (i + "&perpage=" + perpage+ "") +  "  title="+i+">"+i+"</a> ");
					}
				}
			}else{
				if(currentPage<3){
					for(int i=1;i<=4;i++){
						if(i==currentPage){
							out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+i+">"+i+"</a> ");
						}else{
							out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ( i + "&perpage=" + perpage+ "") +  "  title="+i+">"+i+"</a> ");
						}
					}
				}else if(currentPage<(pageCount-2)){
					out.println("...");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-2)+">"+(currentPage-2)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-1)+">"+(currentPage-1)+"</a> ");
					out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+currentPage+">"+currentPage+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage +1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage+1)+">"+(currentPage+1)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage +2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage+2)+">"+(currentPage+2)+"</a> ");
					out.println("...");
				}else if(currentPage==(pageCount-2)){
					out.println("...");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-2)+">"+(currentPage-2)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-1)+">"+(currentPage-1)+"</a> ");
					out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+currentPage+">"+currentPage+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage +1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage+1)+">"+(currentPage+1)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage +2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage+2)+">"+(currentPage+2)+"</a> ");
				}else if(currentPage==(pageCount-1)){
					out.println("...");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -3)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-3)+">"+(currentPage-3)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-2)+">"+(currentPage-2)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -1)+  "&perpage=" + perpage+ "") +  "  title="+(currentPage-1)+">"+(currentPage-1)+"</a> ");
					out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+(currentPage)+">"+(currentPage)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage +1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage+1)+">"+(currentPage+1)+"</a> ");
				}
				else{
					out.println("...");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -4)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-4)+">"+(currentPage-4)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -3)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-3)+">"+(currentPage-3)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -2)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-2)+">"+(currentPage-2)+"</a> ");
					out.println("<a class='btn btn-xs btn-info' href="+ url +"currentPage="+ ((currentPage -1)+ "&perpage=" + perpage+ "") +  "  title="+(currentPage-1)+">"+(currentPage-1)+"</a> ");
					out.println("<a class='btn btn-xs btn-success' href='javascript:void(0)'  title="+currentPage+">"+currentPage+"</a> ");
				}
			}
			out.println("<a class='btn btn-xs btn-info' href=" + url + "currentPage="+ (currentPage + 1 + "") + "&perpage=" + perpage+ " title=下一页>下一页 </a>");
			out.println("&nbsp;&nbsp;");
			out.println("<a class='btn btn-xs btn-info' href="+ url + "currentPage=" + (pageCount + "")+ "&perpage=" + perpage + " title=尾页>尾&nbsp;&nbsp页 <i class='fa fa-forward'></i></a>"); 
		}
		out.println("&nbsp;&nbsp;");
		out.println("共【"+pageBean.getRowCount()+"】条　显示第【 " + currentPage + "/" + pageCount + "】 页");
		out.println("&nbsp;&nbsp;");
		out.println("每页 ");
		out.println("<div class='form-group'><input type='text' class='form-control input-sm' maxlength=3 value="+perpage+" " +
				"style='font-size: 11;width: 50px;' " +
				"onblur='javaScript: var re = /^[1-9]+[0-9]*]*$/;" +
				"if(re.test(this.value)){if(this.value>100){alert(\"最大值为100\");this.value=\"\";this.focus();return;}" +
				"location.href=\""+url+"perpage=\"+this.value;}" +
				"else{alert(\"请输入整数（例如：3,5,6）!\");this.value=\"\";this.focus();}' />");
		out.println("</div> 条");
		out.println("跳到");
		String textid="tagt"+tagnum;
//		System.out.println(textid);
		out.println("<div class='form-group'><input type='text' class='form-control input-sm' id="+textid+"  maxlength=5  style='font-size: 11;width: 50px; '/>");
		out.println("页");
		out.println("<input type='button' class='btn btn-xs btn-info'  value='跳到' onclick='javaScript:var re = /^[1-9]+[0-9]*]*$/;" +
						"if(re.test(document.getElementById(\""+textid+"\").value)){location.href=\""
						+ url
						+ "perpage="
						+ perpage
						+ "&currentPage=\"+document.getElementById(\""+textid+"\").value;}" +
				"else{alert(\"请输入整数（例如：3,5,6）!\");this.focus();return;}' /></div>");
		out.println("</form>");
		}
	}

	

	

}
